import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class Jiami {
    public static void main(String[] args) throws IOException {
        /*
         * 写一个文件编写加解密的程序 Encrypt.java，
         * 该程序给文件进行加密，加密方法是，对文件中每一
         * 个字节进行一些位取反操作；解密与加密一样，
         * 对已加密的文件再进行一次位取反操作
         */
//  jiaMi("D:\\aa.jpg");
        jieMi("D:\\aa.txt");
    }
    public static void jiaMi(String name) throws IOException{
        File f1=new File(name);
        File f2=new File(name+1);

        FileInputStream fis=new FileInputStream(f1);
        FileOutputStream fos=new FileOutputStream(f2);
        byte[] datas=new byte[5];
        while(true){
            int readCount = fis.read(datas);
            if(readCount==-1){
                break;
            }else{
                for(int i=0;i<readCount;i++){
                    datas[i]=(byte)~datas[i];
                }
                fos.write(datas,0,readCount);
            }
        }
        fis.close();
        fos.close();
        f1.delete();
        f2.renameTo(f1);
    }
    public static void jieMi(String name) throws IOException{
        File f1=new File(name);
        File f2=new File(name+1);

        FileInputStream fis=new FileInputStream(f1);
        FileOutputStream fos=new FileOutputStream(f2);

        byte[] datas=new byte[5];
        while(true){
            int readCount = fis.read(datas);
            if(readCount==-1){
                break;
            }else{
                for(int i=0;i<readCount;i++){
                    datas[i]=(byte)~datas[i];
                }
                fos.write(datas,0,readCount);
            }
        }
        fis.close();
        fos.close();
        f1.delete();
        f2.renameTo(f1);
    }
}

